﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JChatCore.Topic;
using JChatData.LogMap;
using JChatData.Helper;

namespace JChatSerVices.TopicServices
{
    public partial class TopicService
    {
        DBConectString _db;
        GetConnect.GetFileConnect _get;

        public TopicService()
        {
            _get = new GetConnect.GetFileConnect();
            string file = _get.GetFile("conn");
            _db = new DBConectString(file);
        }

        #region GetData
        public List<Topic>GetAllTopic()
        {
            var _topic = (from t in _db._context.JChat_Topic
                          select t).ToList();
            return _topic;
        }

        public List<Topic> GetTopicByID(int _id)
        {
            var _topic = (from t in _db._context.JChat_Topic
                          where t.TopicID == _id
                          select t).ToList();
            return _topic;
        }
        #endregion

        #region Add/Update/Delete
        public bool AddTopic(Topic _topic)
        {
            try
            {
                _db._context.JChat_Topic.AddObject(_topic);
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateTopic(int _id, Topic _topictable)
        {
            try
            {
                var _topic = (from t in _db._context.JChat_Topic
                             where t.TopicID == _id
                             select t).SingleOrDefault();
                _topic.Title = _topictable.Title;
                _topic.SystemName = _topictable.SystemName;
                _topic.MetaKeyword = _topictable.MetaKeyword;
                _topic.Body = _topictable.Body;
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteTopic(int _id)
        {
            try
            {
                var _topic = (from t in _db._context.JChat_Topic
                              where t.TopicID == _id
                              select t).SingleOrDefault();
                _db._context.DeleteObject(_topic);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
